﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Revit.MEP.Opening
{
	public class ConfigDataOperate
	{
		public static List<string> GetDrivePipeTypeName()
		{
			return new List<string>
			{
				"柔性防水套管A",
				"柔性防水套管B",
				"刚性防水套管A",
				"刚性防水套管B",
				"刚性防水套管C",
				"穿墙套管",
				"无"
			};
		}

		public static List<string> GetDocAndLinkDocFileName(Autodesk.Revit.DB.Document doc)
		{
			List<string> list = new List<string>();
			try
			{
				string item = XmlUtils.SplitPathName(doc.PathName);
				list.Add(item);
				foreach (LinkFile linkFile in LinkFile.GetLinkFiles(doc))
				{
					item = XmlUtils.SplitPathName(linkFile.FileName);
					list.Add(item);
				}
			}
			catch
			{
			}
			return list;
		}

		public static double ConvertString2Double(string strVal)
		{
			if (!string.IsNullOrEmpty(strVal))
			{
				return Convert.ToDouble(strVal);
			}
			return 0.0;
		}

		public static int ConvertString2Int32(string strVal)
		{
			if (!string.IsNullOrEmpty(strVal))
			{
				return Convert.ToInt32(strVal);
			}
			return 0;
		}

		public static string InitSteelDrivePipeSizeTableFile()
		{
			return ConfigDataOperate.InitInstallDataFile2UserData(ConfigDataOperate.m_strSteelDrivePipeSizeTableFileName);
		}

		public static string InitConfigDataFile()
		{
			return ConfigDataOperate.InitInstallDataFile2UserData(ConfigDataOperate.m_strConfigOfferDataFileName);
		}

		public static string InitOpenHoleDataConfigFile()
		{
			return ConfigDataOperate.InitInstallDataFile2UserData(ConfigDataOperate.m_strOpenHoleConfigFileName);
		}

		public static List<SteelDrivePipeData> ReadSteelDrivePipeSizeConfigData()
		{
			List<SteelDrivePipeData> list = new List<SteelDrivePipeData>();
			try
			{
				string filename = ConfigDataOperate.InitSteelDrivePipeSizeTableFile();
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(filename);
				foreach (object obj in xmlDocument.GetElementsByTagName("SteelDrivePipeItem"))
				{
					XmlAttributeCollection attributes = ((XmlNode)obj).Attributes;
					if (attributes != null)
					{
						SteelDrivePipeData steelDrivePipeData = new SteelDrivePipeData();
						foreach (object obj2 in attributes)
						{
							XmlAttribute xmlAttribute = (XmlAttribute)obj2;
							string name = xmlAttribute.Name;
							if (!(name == "DrivePipeDn"))
							{
								if (!(name == "MetalPipeDn"))
								{
									if (name == "PlasticPipeDn")
									{
										steelDrivePipeData.PlasticPipeDn = xmlAttribute.Value;
									}
								}
								else
								{
									steelDrivePipeData.MetalPipeDn = xmlAttribute.Value;
								}
							}
							else
							{
								steelDrivePipeData.DrivePipeDn = xmlAttribute.Value;
							}
						}
						list.Add(steelDrivePipeData);
					}
				}
				list.Sort();
			}
			catch
			{
			}
			return list;
		}

		public static bool WriteSteelDrivePipeSizeConfigData(List<SteelDrivePipeData> lstData)
		{
			try
			{
				string filename = ConfigDataOperate.InitSteelDrivePipeSizeTableFile();
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.CreateXmlDeclaration("1.0", "utf-8", "yes");
				XmlNode xmlNode = xmlDocument.CreateElement("SteelDrivePipeSizeTable");
				foreach (SteelDrivePipeData steelDrivePipeData in lstData)
				{
					XmlNode xmlNode2 = xmlDocument.CreateElement("SteelDrivePipeItem");
					XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("DrivePipeDn");
					xmlAttribute.Value = steelDrivePipeData.DrivePipeDn;
					xmlNode2.Attributes.Append(xmlAttribute);
					XmlAttribute xmlAttribute2 = xmlDocument.CreateAttribute("MetalPipeDn");
					xmlAttribute2.Value = steelDrivePipeData.MetalPipeDn;
					xmlNode2.Attributes.Append(xmlAttribute2);
					XmlAttribute xmlAttribute3 = xmlDocument.CreateAttribute("PlasticPipeDn");
					xmlAttribute3.Value = steelDrivePipeData.PlasticPipeDn;
					xmlNode2.Attributes.Append(xmlAttribute3);
					xmlNode.AppendChild(xmlNode2);
				}
				xmlDocument.AppendChild(xmlNode);
				xmlDocument.Save(filename);
				return true;
			}
			catch
			{
			}
			return false;
		}

		private static string InitInstallDataFile2UserData(string strDataFileName)
		{
			try
			{
				string text = Product.UserDataLocation + strDataFileName;
				if (!new FileInfo(text).Exists)
				{
					new FileInfo(Product.DataLocation + strDataFileName).CopyTo(text);
				}
				return text;
			}
			catch
			{
				YJKMessageBox.Show("初始化用户数据文件(" + strDataFileName + ")失败!");
			}
			return "";
		}

		public static string[] arDrivePipeDimPartName = new string[]
		{
			"D1",
			"D2",
			"D3",
			"D4",
			"D5",
			"D6",
			"D7",
			"D8"
		};

		public static string[] arOpeningLevelBase = new string[]
		{
			"底标高",
			"中心标高",
			"顶标高"
		};

		private static string m_strSteelDrivePipeSizeTableFileName = "\\SteelDrivePipeSizeTable.xml";

		private static string m_strConfigOfferDataFileName = "\\ConfigOfferData.xml";

		private static string m_strOpenHoleConfigFileName = "\\OpenHoleConfigData.xml";

		private static string m_strSynergyData = "\\SynergyData.xml";

		public enum EOperMode
		{
			eOperLocal,
			eOperServer
		}

		public enum EDrivePipeType
		{
			eWaterProofFlexibleA,
			eWaterProofFlexibleB,
			eWaterProofInflexibleA,
			eWaterProofInflexibleB,
			eWaterProofInflexibleC,
			eSteel,
			eNone
		}

		public enum EDimHoleStyle
		{
			eDimStyleByTable,
			eDimStyleByLabel
		}

		public enum EOpeningLevelBase
		{
			eLevelBaseBottom,
			eLevelBaseCenter,
			eLevelBaseTop
		}

		public enum EWaterProofFlexiblePipeDimPart
		{
			eDimPartD1,
			eDimPartD2,
			eDimPartD3,
			eDimPartD4,
			eDimPartD5,
			eCount
		}

		public enum EWaterProofInflexiblePipeDimPart
		{
			eDimPartD1,
			eDimPartD2,
			eDimPartD3,
			eDimPartD4,
			eCount
		}

		public enum ESealFlexiblePipeDimPart
		{
			eDimPartD1,
			eDimPartD2,
			eDimPartD3,
			eDimPartD4,
			eDimPartD5,
			eDimPartD6,
			eDimPartD7,
			eCount
		}

		public enum ESealInflexiblePipeDimPart
		{
			eDimPartD1,
			eDimPartD2,
			eDimPartD3,
			eDimPartD4,
			eCount
		}

		public class OfferConfigData : XmlConfigDataBase
		{
			public OfferConfigData(string strXmlFilePath) : base(strXmlFilePath)
			{
			}

			protected override void InitDataByDefaultConfig()
			{
				this.OperMode = ConfigDataOperate.EOperMode.eOperLocal;
				this.LocalOperDataFilePath = Product.UserDataLocation + ConfigDataOperate.m_strSynergyData;
				this.CircelOpeningDist2Pipe = 50.0;
				this.RectOpeningDist2Pipe = 50.0;
				this.MergeOpeningMaxDist = 300.0;
				this.MergeOpeningExtend = 200.0;
				this.WaterProofTubleDefType = ConfigDataOperate.EDrivePipeType.eWaterProofFlexibleA;
				this.FlexWPDrivePipeDimType = ConfigDataOperate.EWaterProofFlexiblePipeDimPart.eDimPartD3;
				this.InFlexWPDrivePipeDimType = ConfigDataOperate.EWaterProofInflexiblePipeDimPart.eDimPartD3;
				this.FlexSealDrivePipeDimType = ConfigDataOperate.ESealFlexiblePipeDimPart.eDimPartD3;
				this.InFlexSealDrivePipeDimType = ConfigDataOperate.ESealInflexiblePipeDimPart.eDimPartD3;
			}

			public ConfigDataOperate.EOperMode OperMode { get; set; }

			public string LocalOperDataFilePath { get; set; }

			public double RectOpeningDist2Pipe { get; set; }

			public double CircelOpeningDist2Pipe { get; set; }

			public double MergeOpeningMaxDist { get; set; }

			public double MergeOpeningExtend { get; set; }

			public ConfigDataOperate.EDrivePipeType WaterProofTubleDefType { get; set; }

			public ConfigDataOperate.EWaterProofFlexiblePipeDimPart FlexWPDrivePipeDimType { get; set; }

			public ConfigDataOperate.EWaterProofInflexiblePipeDimPart InFlexWPDrivePipeDimType { get; set; }

			public ConfigDataOperate.ESealFlexiblePipeDimPart FlexSealDrivePipeDimType { get; set; }

			public ConfigDataOperate.ESealInflexiblePipeDimPart InFlexSealDrivePipeDimType { get; set; }
		}

		public class OpenHoleConfigData : XmlConfigDataBase
		{
			public OpenHoleConfigData(string strXmlFilePath) : base(strXmlFilePath)
			{
			}

			protected override void InitDataByDefaultConfig()
			{
				this.OperMode = ConfigDataOperate.EOperMode.eOperLocal;
				this.LocalOperDataFilePath = Product.UserDataLocation + ConfigDataOperate.m_strSynergyData;
				this.DimHoleStyle = ConfigDataOperate.EDimHoleStyle.eDimStyleByTable;
				this.DimPrefixItem = "洞";
				this.RectOpeningLevelBase = ConfigDataOperate.EOpeningLevelBase.eLevelBaseBottom;
				this.CircleOpeningLevelBase = ConfigDataOperate.EOpeningLevelBase.eLevelBaseCenter;
				this.DimOpening = false;
				this.DimMajor = false;
				this.ArchOpeningMinSize = 0.0;
				this.ArchOpeningDimMinSize = 0.0;
			}

			public ConfigDataOperate.EOperMode OperMode { get; set; }

			public string LocalOperDataFilePath { get; set; }

			public ConfigDataOperate.EDimHoleStyle DimHoleStyle { get; set; }

			public string DimPrefixItem { get; set; }

			public ConfigDataOperate.EOpeningLevelBase RectOpeningLevelBase { get; set; }

			public ConfigDataOperate.EOpeningLevelBase CircleOpeningLevelBase { get; set; }

			public bool DimOpening { get; set; }

			public bool DimMajor { get; set; }

			public double ArchOpeningMinSize { get; set; }

			public double ArchOpeningDimMinSize { get; set; }
		}
	}
}
